﻿using System.IO;
using System.Xml;

namespace DMirrorMySQL
{
    public class XMLWorker
    {
        private XmlDocument doc;
        private DMirror dm;
        //      private HashSet<string> tagHold;
        private int siteID;
        public int total = 0;
        public int offset = 0;

        public XMLWorker(ref DMirror db, int SiteID)
        {
            dm = db;
            siteID = SiteID;
        }

        /// <summary>
        /// 导入XML
        /// </summary>
        /// <param name="xmlSource">XML流</param>
        public string ImportXML(string xmlSource)
        {
            int cnt = 0;
            int tagcount = 0;
            int tagassign = 0;
            int c = 0;
            //    tagHold = new HashSet<string>();
            doc = new XmlDocument();
            //int total = 0;
            //int offset = 0;

            string[] tags;
            DPost d;
            FileStream fs = new FileStream(xmlSource, FileMode.Open);
            doc.Load(fs);
            XmlNode posts = doc.DocumentElement.SelectSingleNode("/posts");

            int.TryParse(posts.Attributes["count"].Value, out total);
            int.TryParse(posts.Attributes["offset"].Value, out offset);
            //toContinue = (total > offset);

            //批量添加tag

            foreach (XmlNode item in posts.SelectNodes("/posts/post"))
            {
                //post 部分
                d = new DPost();
                d.SiteID = siteID;
                int.TryParse(item.Attributes["id"].Value, out d.PostID);
                int.TryParse(item.Attributes["height"].Value, out d.Height);
                int.TryParse(item.Attributes["width"].Value, out d.Width);
                int.TryParse(item.Attributes["file_size"].Value, out d.Size);
                d.MD5 = item.Attributes["md5"].Value;
                d.tURL = item.Attributes["preview_url"].Value;
                d.URL = item.Attributes["file_url"].Value;
                dm.addPost(d);
                //tag部分
                tags = item.Attributes["tags"].Value.Split(' ');
                //clean tag
                dm.cleanTag(d.PostID, d.SiteID);
                foreach (string t in tags)
                {
                    c = dm.assignTag(d.PostID, d.SiteID, t);
                    if (c == 1)
                    {
                        tagassign++; tagcount++;
                    }
                    else
                    {
                        tagassign++;
                    }
                }
                cnt++;
            }

            fs.Close();
            fs.Dispose();

            dm.errorLog("importXML", "using " + xmlSource, string.Format("succeed :{0}_offset_{1}, {2} added", total.ToString(), offset.ToString(), cnt.ToString()));
            //if (offset == 0) toContinue = false; else toContinue = true;
            return string.Format("{0} posts , {1} tags assigned, {2} tags created", cnt.ToString(), tagassign.ToString(), tagcount.ToString());
        }

        //public void ImportXML_IgnoreTags(string xmlSource)
        //{
        //    int cnt = 0;

        //    //tagHold = new HashSet<string>();
        //    doc = new XmlDocument();
        //    //int total = 0;
        //    //int offset = 0;

        //   // string[] tags;
        //    DPost d;
        //    FileStream fs = new FileStream(xmlSource, FileMode.Open);
        //    doc.Load(fs);
        //    XmlNode posts = doc.DocumentElement.SelectSingleNode("/posts");

        //    int.TryParse(posts.Attributes["count"].Value, out total);
        //    int.TryParse(posts.Attributes["offset"].Value, out offset);
        //    //toContinue = (total > offset);
        //    DPost[] dx = new DPost[1000];
        //    //批量添加tag

        //    foreach (XmlNode item in posts.SelectNodes("/posts/post"))
        //    {
        //        //post 部分
        //        d = new DPost();
        //        d.SiteID = siteID;
        //        int.TryParse(item.Attributes["id"].Value, out d.PostID);
        //        int.TryParse(item.Attributes["height"].Value, out d.Height);
        //        int.TryParse(item.Attributes["width"].Value, out d.Width);
        //        int.TryParse(item.Attributes["file_size"].Value, out d.Size);
        //        d.MD5 = item.Attributes["md5"].Value;
        //        d.tURL = item.Attributes["preview_url"].Value;
        //        d.URL = item.Attributes["file_url"].Value;
        //        dx[cnt] = d;
        //       // dm.addPost(d);
        //        //tag部分
        //        /*
        //        tags = item.Attributes["tags"].Value.Split(' ');
        //        foreach (string t in tags)
        //        {
        //            dm.assignTag(d.PostID, d.SiteID, t);
        //        }*/

        //        cnt++;
        //    }
        //    dm.addPost(ref dx);

        //    fs.Close();
        //    fs.Dispose();

        //    dm.errorLog("importXML", "using " + xmlSource, string.Format("succeed :{0}_offset_{1}, {2} added", total.ToString(), offset.ToString(), cnt.ToString()));
        //    //if (offset == 0) toContinue = false; else toContinue = true;

        //}
    }

    public class LSTWorker
    {
        static public void importThumbList(string site_id, ref DMirror db, string thumb_list_fn)
        {
            string[] k = File.ReadAllLines(thumb_list_fn);
            db.syncThumb(ref k, site_id);
        }
    }
}